﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace Curling
{
    public partial class ScoresPage : PhoneApplicationPage
    {
        bool GoBack = false;

        readonly IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

        ObservableCollection<ScoreTableItem> LocalScoreTable;
        ObservableCollection<ScoreTableItem> InternetScoreTable;

        public ScoresPage()
        {
            InitializeComponent();

            FadeOut.Completed += new EventHandler(FadeOut_Completed);
        }

        private void PhoneApplicationPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (!GoBack)
            {
                FadeOut.Begin();
                GoBack = true;
            }

            e.Cancel = true;  
        }

        void FadeOut_Completed(object sender, EventArgs e)
        {
            NavigationService.GoBack();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            FadeIn.Begin();
        }

        private void PhoneApplicationPage_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            if (!settings.Contains("InternetScoreTable"))
            {
                settings["InternetScoreTable"] = new ObservableCollection<ScoreTableItem>();
            }

            if (!settings.Contains("MatchesScoreTable"))
            {
                settings["MatchesScoreTable"] = new ObservableCollection<ScoreTableItem>();
            }

            LocalScoreTable = new ObservableCollection<ScoreTableItem>();
            LocalScoreList.ItemsSource = LocalScoreTable;

            InternetScoreTable = new ObservableCollection<ScoreTableItem>();
            InternetScoreList.ItemsSource = InternetScoreTable;

            BackgroundWorker backgroundWorker = new BackgroundWorker();
            backgroundWorker.DoWork += new DoWorkEventHandler(LoadScoreAsync);
            backgroundWorker.RunWorkerAsync();            
        }

        private void LoadScoreAsync(object sender, DoWorkEventArgs e)
        {
            ObservableCollection<ScoreTableItem> InternetScoreTableLoad = (ObservableCollection<ScoreTableItem>)settings["InternetScoreTable"];

            foreach (ScoreTableItem scoreTableItem in InternetScoreTableLoad)
            {
                ScoreTableItem ScoreTableItem = scoreTableItem;
                
                Dispatcher.BeginInvoke(() =>
                {
                    InternetScoreTable.Add(ScoreTableItem);
                });
            }

            ObservableCollection<ScoreTableItem> LocalScoreTableLoad = (ObservableCollection<ScoreTableItem>)settings["MatchesScoreTable"];

            foreach (ScoreTableItem scoreTableItem in LocalScoreTableLoad)
            {
                ScoreTableItem ScoreTableItem = scoreTableItem;

                Dispatcher.BeginInvoke(() =>
                {
                    LocalScoreTable.Add(ScoreTableItem);
                });
            }  
        }
    }
}
